import Vue from 'vue'
import Vuex from 'vuex'
import serviceControl from '@/common/service_control'
import { Toast } from 'vant'

Vue.use(Vuex)
// 默认配置
let conf = {
  playerState: {
    playBackStatus: false, //播放状态
    playData: {            //全局播放信息
      songId: 0,           //音乐ID
      music_title: "我是歌手",         //歌曲标题
      music_singer: "zx",        //歌曲演唱的歌手
      music_now_play_time: 0,  //歌曲当前播放时间
      music_duration_time: 120,  //歌曲总时长
      lrc_now_line_str: "只怕我自己爱上你",     //现在播放的歌词，一句
      lrc_now_line_number: 0,  //歌词当前是播放到了第几行
      lrc_list: [],            //总歌词列表
    },
  },
  playList: {
    playHistory: [],
    playNowList: [33894312, 569213220]
  },
  DataCacheMap: {
    simpleData: {
      33894312: {
        title: "情非得已",
        singer: "童声",
        headUrl: "http://p4.music.126.net/tgkN397ohC6XpqRRHzndLw==/2925800441997173.jpg?param=200y200",
        fee: 0, //0表示不收费,1表示收费
        playUrl: { //播放信息
          expire: 0,    //过期时间，api默认是20分钟
          url: "",      //缓存的播放地址
        }
      },
      569213220: {
        title: "像我这样的人",
        singer: "毛不易",
        headUrl: "http://p2.music.126.net/vmCcDvD1H04e9gm97xsCqg==/109951163350929740.jpg?param=130y130",
        fee: 0,
      }
    },
    LrcData: {

    },

  }
}

// 从存储中加载配置
if (sessionStorage.getItem('store.state')) {
  conf = JSON.parse(sessionStorage.getItem('store.state'))
}
conf.playerState.playBackStatus = false; //设置默认播放状态为false

export default new Vuex.Store({
  state: conf,
  getters: {
  },
  mutations: {  //同步方式调用
    mutateSetPlayerState(_, val) {
      serviceControl.play(val["val"])
    },
    mutateSetPlayData(state, data) {
      // state.playerState.music_now_play_time = data["val"]
      data["id"] = parseInt(data["id"])
      if (data["title"] == null) {
        // 播放现有列表的歌曲？
        if (state.DataCacheMap.simpleData[data["id"]] == null) {
          // 缓存为空，指令无效
          return;
        }
        serviceControl.playId(data["id"]);
        let mInfo = state.DataCacheMap.simpleData[data["id"]]
        Toast("正在播放 " + mInfo["singer"] + " - " + mInfo["title"])
        return;
      }
      state.DataCacheMap.simpleData[data["id"]] = {
        "title": data["title"],
        "headUrl": data["headUrl"],
        "singer": data["singer"],
      }
      if (data['insert'] === true) {
        do {
          //下一首播放模式
          let ind = state.playList.playNowList.indexOf(data["id"])
          if (ind >= 0) {
            if (this.state.playerState.playData['songId'] == data['id']) {
              break;
            }
            // 删除已存在的位置
            this.state.playList.playNowList.splice(ind, 1);
          }
          let pos = this.state.playList.playNowList.indexOf(this.state.playerState.playData['songId']) + 1
          this.state.playList.playNowList.splice(pos, 0, data['id'])
          // console.log('已经添加了下一首播放')
        } while (data == null);

      } else if (this.state.playList.playNowList.indexOf(data["id"]) < 0) {
        this.state.playList.playNowList.push(data["id"])
      }

      if (data["right"]) {
        serviceControl.playId(data["id"]);
        let mInfo = state.DataCacheMap.simpleData[data["id"]]
        Toast("正在播放 " + mInfo["singer"] + " - " + mInfo["title"])
      } else {
        serviceControl.action_updatePlayInfo(data["id"]);
      }
    },

    //传入播放指令，比如 next,prev
    mutateSetPlayAction(_, val) {
      switch (val) {
        case "next":
          serviceControl.action_play_next();
          break;
        case "prev":
          serviceControl.action_play_prev();
          break;

        default:
          break;
      }
    },
    mutateSetPlayProgress(_, data) {
      let type = data["type"];
      let val = data["val"];
      switch (type) {
        case "sec":
        case 0:
          type = 0;
          break;
        case "lyric":
        case 1:
          type = 1;
          break;
        default:
          return;
      }
      serviceControl.action_move_playTime(type, val)
    },
    mutateListControl(state, val) {
      if (val["type"] == "history") {
        state.playList.playHistory = []
      } else if (val["type"] == "play") {
        state.playList.playNowList = []
      }

    }

  },
  actions: {    //异步方式

    serviceSetPlayerState(context, val) {//设置播放状态
      context.commit('mutateSetPlayerState', val)
    },
    serviceSetPlayData(context, val) {//设置播放数据
      // data={"id":"",singer:"",title:"",headUrl:""}
      context.commit('mutateSetPlayData', val)
    },
    serviceSetPlayAction(context, data) {//传入播放指令，比如 next,prev
      context.commit('mutateSetPlayAction', data);
    },
    serviceSetPlayProgress(context, data) {

      context.commit('mutateSetPlayProgress', data);
    },
    serviceListControl(context, data) {
      context.commit('mutateListControl', data);
    }

  },
  modules: {
  }
})
window.store = conf
serviceControl.initData()
